package edu.msg.jthr.backend.bean;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import edu.msg.jthr.backend.exception.ServiceException;
import edu.msg.jthr.backend.model.User;
import edu.msg.jthr.backend.repository.exception.RepositoryException;
import edu.msg.jthr.backend.repository.service.UserRepository;
import edu.msg.jthr.backend.service.UserService;

@Stateless (name = "UserServiceBean", mappedName = "ejb/UserService")
public class UserServiceBean implements UserService {

	@EJB
	private UserRepository userRepository;
	private static Logger LOG = LoggerFactory.getLogger(UserServiceBean.class);

	@Override
	public List<User> getAll() throws ServiceException {
		List<User> userList = new ArrayList<User>();
		
		try {
			userList = userRepository.getAll();
		} catch(RepositoryException e) {
			LOG.error("Get all users failure!", e);
			throw new ServiceException("User getAll failure!", e);
		}
		
		return userList;
	}

	@Override
	public User getById(Long id) throws ServiceException {
		try {
			return userRepository.getById(id);
		}catch(RepositoryException e) {
			LOG.error("User getById failure!", e);
			throw new ServiceException("User getById failure!", e);
		}
	}

	@SuppressWarnings("restriction")
	@Override
	public void create(User obj) throws ServiceException {
		try{
			MessageDigest md = MessageDigest.getInstance("SHA-256");
			md.update(obj.getPassword().getBytes("UTF-8"));
			byte[] passwordDigest = md.digest();
			String encodedPasswordHash = new sun.misc.BASE64Encoder().encode(passwordDigest);
			System.out.println(encodedPasswordHash+"@@@@@ Encoded password @@@@@");
			obj.setPassword(encodedPasswordHash);
			userRepository.create(obj);
		}catch(RepositoryException e) {
			LOG.error("User create failure!", e);
			throw new ServiceException("User create failure!", e);
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public User update(User obj) throws ServiceException {
		try{
			return userRepository.update(obj);
		}catch(RepositoryException e) {
			LOG.error("User update failure!", e);
			throw new ServiceException("User update failure!", e);
		}
	}

	@Override
	public void delete(User obj) throws ServiceException {
		try{
			userRepository.delete(obj);
		}catch(RepositoryException e) {
			LOG.error("User delete failure!", e);
			throw new ServiceException("User delete failure!", e);
		}
	}

	@Override
	public User getUserByUserName(String userName) {
		User user = new User();
		try {
			user = userRepository.getUserByUserName(userName);
		} catch(RepositoryException e) {
			LOG.error("Get all users failure!", e);
			throw new ServiceException("User getAll failure!", e);
		}
		return user;
	}

}
